// --------------------------------------------------------------------------------------------------------------------
// <copyright file="Application.cs" company="Open Trader">
//   Copyright (c) David Denis (david.denis@systemathics.com)
// </copyright>
// <summary>
//   |  Open Trader - The Open Source Systematic Trading Platform
//   |
//   |  This program is free software: you can redistribute it and/or modify
//   |  it under the terms of the GNU General Public License as published by
//   |  the Free Software Foundation, either version 2 of the License, or
//   |  (at your option) any later version.
//   |
//   |  This program is distributed in the hope that it will be useful,
//   |  but WITHOUT ANY WARRANTY; without even the implied warranty of
//   |  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//   |  GNU General Public License for more details.
//   |
//   |  You should have received a copy of the GNU General Public License
//   |  along with this program.  If not, see http://www.gnu.org/licenses
//   |
//   |  Up to date informations about Open Trader can be found at :
//   |    http://opentrader.org
//   |    http://opentrader.codeplex.com
//   |
//   |  For professional services, please visit us at :
//   |    http://www.systemathics.com
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Org.OpenTrader.Framework.LiveObjects
{
    #region Using Directives

    using System;
    using System.Collections.Generic;
    using System.Linq;

    using Org.OpenTrader.Framework.Enums;
    using Org.OpenTrader.Framework.Events;
    using Org.OpenTrader.Framework.Interfaces.UI;
    using Org.OpenTrader.Framework.LiveObjects.Interfaces;

    #endregion

    /// <summary>
    /// The this.
    /// </summary>
    public abstract class Application : LiveObject, IApplication
    {
        // Service by name
        #region Constants and Fields

        /// <summary>
        /// The services.
        /// </summary>
        private readonly IDictionary<string, IService> services = new Dictionary<string, IService>();

        #endregion

        #region Constructors and Destructors

        /// <summary>
        /// Initializes a new instance of the <see cref="Application"/> class.
        /// </summary>
        /// <param name="descriptor">
        /// The descriptor.
        /// </param>
        /// <param name="container">
        /// The container.
        /// </param>
        public Application(ILiveObjectDescriptor descriptor, ILiveObjectContainer container)
            : base(descriptor, container)
        {
        }

        #endregion

        #region Properties

        /// <summary>
        /// Gets or sets ContextName.
        /// </summary>
        public string ContextName { get; set; }

        // The root control

        /// <summary>
        /// Gets or sets RootControl.
        /// </summary>
        public IUIRootControl RootControl { get; set; }

        /// <summary>
        /// Gets Services.
        /// </summary>
        public IService[] Services
        {
            get
            {
                return this.services.Values.ToArray();
            }
        }

        #endregion

        #region Indexers

        /// <summary>
        /// The this.
        /// </summary>
        /// <param name="name">
        /// The name.
        /// </param>
        public IService this[string name]
        {
            get
            {
                if (this.services.ContainsKey(name))
                {
                    return this.services[name];
                }

                return null;
            }
        }

        #endregion

        #region Public Methods

        /// <summary>
        /// The activate.
        /// </summary>
        /// <param name="application">
        /// The this.
        /// </param>
        public void Activate()
        {
            // Change Current Directory for this domain
            Environment.CurrentDirectory = this.Container.Descriptor.WorkingDirectory;

            this.LiveObjectsEventCB.RaiseEvent(this, new LiveObjectsEventArgs("Activating " + this.Descriptor.Name));

            // Activate all services
            foreach (var service in this.Services)
            {
                service.LiveObjectsEventCB.Chain(this.LiveObjectsEventCB);
                (service as Service).Activate(this, service);
            }

            // Send the activatiion event on Application
            this.OnActivation(AppDomain.CurrentDomain, new ActivationEventArgs.CreateDestroy(EActivationType.Activation));

            this.LiveObjectsEventCB.RaiseEvent(this, new LiveObjectsEventArgs("Activated " + this.Descriptor.Name));
        }

        /// <summary>
        /// The add service.
        /// </summary>
        /// <param name="name">
        /// The name.
        /// </param>
        /// <param name="service">
        /// The service.
        /// </param>
        public void AddService(string name, IService service)
        {
            this.services[name] = service;
        }

        /// <summary>
        /// The desactivate.
        /// </summary>
        /// <param name="application">
        /// The this.
        /// </param>
        public void Desactivate()
        {
            // Change Current Directory for this domain
            Environment.CurrentDirectory = this.Container.Descriptor.WorkingDirectory;

            this.LiveObjectsEventCB.RaiseEvent(this, new LiveObjectsEventArgs("Desactivating " + this.Descriptor.Name));

            // Activate all services
            foreach (var service in this.Services)
            {
                (service as Service).Desactivate(this, service);
                service.LiveObjectsEventCB.UnChain(this.LiveObjectsEventCB);
            }

            // Send the activatiion event on Application
            this.OnActivation(AppDomain.CurrentDomain, new ActivationEventArgs.CreateDestroy(EActivationType.Desactivation));

            this.LiveObjectsEventCB.RaiseEvent(this, new LiveObjectsEventArgs("Desactivated " + this.Descriptor.Name));
        }

        #endregion
    }
}